<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>Source code</title>
<link rel="stylesheet" type="text/css" href="../../../../../../../stylesheet.css" title="Style">
</head>
<body>
<div class="sourceContainer">
<pre><span class="sourceLineNo">001</span>/*<a name="line.1"></a>
<span class="sourceLineNo">002</span> * Licensed to the Apache Software Foundation (ASF) under one or more<a name="line.2"></a>
<span class="sourceLineNo">003</span> * contributor license agreements.  See the NOTICE file distributed with<a name="line.3"></a>
<span class="sourceLineNo">004</span> * this work for additional information regarding copyright ownership.<a name="line.4"></a>
<span class="sourceLineNo">005</span> * The ASF licenses this file to You under the Apache License, Version 2.0<a name="line.5"></a>
<span class="sourceLineNo">006</span> * (the "License"); you may not use this file except in compliance with<a name="line.6"></a>
<span class="sourceLineNo">007</span> * the License.  You may obtain a copy of the License at<a name="line.7"></a>
<span class="sourceLineNo">008</span> *<a name="line.8"></a>
<span class="sourceLineNo">009</span> *      http://www.apache.org/licenses/LICENSE-2.0<a name="line.9"></a>
<span class="sourceLineNo">010</span> *<a name="line.10"></a>
<span class="sourceLineNo">011</span> * Unless required by applicable law or agreed to in writing, software<a name="line.11"></a>
<span class="sourceLineNo">012</span> * distributed under the License is distributed on an "AS IS" BASIS,<a name="line.12"></a>
<span class="sourceLineNo">013</span> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.<a name="line.13"></a>
<span class="sourceLineNo">014</span> * See the License for the specific language governing permissions and<a name="line.14"></a>
<span class="sourceLineNo">015</span> * limitations under the License.<a name="line.15"></a>
<span class="sourceLineNo">016</span> */<a name="line.16"></a>
<span class="sourceLineNo">017</span><a name="line.17"></a>
<span class="sourceLineNo">018</span>package org.apache.commons.codec.language.bm;<a name="line.18"></a>
<span class="sourceLineNo">019</span><a name="line.19"></a>
<span class="sourceLineNo">020</span>import org.apache.commons.codec.EncoderException;<a name="line.20"></a>
<span class="sourceLineNo">021</span>import org.apache.commons.codec.StringEncoder;<a name="line.21"></a>
<span class="sourceLineNo">022</span><a name="line.22"></a>
<span class="sourceLineNo">023</span>/**<a name="line.23"></a>
<span class="sourceLineNo">024</span> * Encodes strings into their Beider-Morse phonetic encoding.<a name="line.24"></a>
<span class="sourceLineNo">025</span> * &lt;p&gt;<a name="line.25"></a>
<span class="sourceLineNo">026</span> * Beider-Morse phonetic encodings are optimised for family names. However, they may be useful for a wide range of<a name="line.26"></a>
<span class="sourceLineNo">027</span> * words.<a name="line.27"></a>
<span class="sourceLineNo">028</span> * &lt;p&gt;<a name="line.28"></a>
<span class="sourceLineNo">029</span> * This encoder is intentionally mutable to allow dynamic configuration through bean properties. As such, it is mutable,<a name="line.29"></a>
<span class="sourceLineNo">030</span> * and may not be thread-safe. If you require a guaranteed thread-safe encoding then use {@link PhoneticEngine}<a name="line.30"></a>
<span class="sourceLineNo">031</span> * directly.<a name="line.31"></a>
<span class="sourceLineNo">032</span> * &lt;p&gt;<a name="line.32"></a>
<span class="sourceLineNo">033</span> * &lt;b&gt;Encoding overview&lt;/b&gt;<a name="line.33"></a>
<span class="sourceLineNo">034</span> * &lt;p&gt;<a name="line.34"></a>
<span class="sourceLineNo">035</span> * Beider-Morse phonetic encodings is a multi-step process. Firstly, a table of rules is consulted to guess what<a name="line.35"></a>
<span class="sourceLineNo">036</span> * language the word comes from. For example, if it ends in "{@code ault}" then it infers that the word is French.<a name="line.36"></a>
<span class="sourceLineNo">037</span> * Next, the word is translated into a phonetic representation using a language-specific phonetics table. Some runs of<a name="line.37"></a>
<span class="sourceLineNo">038</span> * letters can be pronounced in multiple ways, and a single run of letters may be potentially broken up into phonemes at<a name="line.38"></a>
<span class="sourceLineNo">039</span> * different places, so this stage results in a set of possible language-specific phonetic representations. Lastly, this<a name="line.39"></a>
<span class="sourceLineNo">040</span> * language-specific phonetic representation is processed by a table of rules that re-writes it phonetically taking into<a name="line.40"></a>
<span class="sourceLineNo">041</span> * account systematic pronunciation differences between languages, to move it towards a pan-indo-european phonetic<a name="line.41"></a>
<span class="sourceLineNo">042</span> * representation. Again, sometimes there are multiple ways this could be done and sometimes things that can be<a name="line.42"></a>
<span class="sourceLineNo">043</span> * pronounced in several ways in the source language have only one way to represent them in this average phonetic<a name="line.43"></a>
<span class="sourceLineNo">044</span> * language, so the result is again a set of phonetic spellings.<a name="line.44"></a>
<span class="sourceLineNo">045</span> * &lt;p&gt;<a name="line.45"></a>
<span class="sourceLineNo">046</span> * Some names are treated as having multiple parts. This can be due to two things. Firstly, they may be hyphenated. In<a name="line.46"></a>
<span class="sourceLineNo">047</span> * this case, each individual hyphenated word is encoded, and then these are combined end-to-end for the final encoding.<a name="line.47"></a>
<span class="sourceLineNo">048</span> * Secondly, some names have standard prefixes, for example, "{@code Mac/Mc}" in Scottish (English) names. As<a name="line.48"></a>
<span class="sourceLineNo">049</span> * sometimes it is ambiguous whether the prefix is intended or is an accident of the spelling, the word is encoded once<a name="line.49"></a>
<span class="sourceLineNo">050</span> * with the prefix and once without it. The resulting encoding contains one and then the other result.<a name="line.50"></a>
<span class="sourceLineNo">051</span> * &lt;p&gt;<a name="line.51"></a>
<span class="sourceLineNo">052</span> * &lt;b&gt;Encoding format&lt;/b&gt;<a name="line.52"></a>
<span class="sourceLineNo">053</span> * &lt;p&gt;<a name="line.53"></a>
<span class="sourceLineNo">054</span> * Individual phonetic spellings of an input word are represented in upper- and lower-case roman characters. Where there<a name="line.54"></a>
<span class="sourceLineNo">055</span> * are multiple possible phonetic representations, these are joined with a pipe ({@code |}) character. If multiple<a name="line.55"></a>
<span class="sourceLineNo">056</span> * hyphenated words where found, or if the word may contain a name prefix, each encoded word is placed in elipses and<a name="line.56"></a>
<span class="sourceLineNo">057</span> * these blocks are then joined with hyphens. For example, "{@code d'ortley}" has a possible prefix. The form<a name="line.57"></a>
<span class="sourceLineNo">058</span> * without prefix encodes to "{@code ortlaj|ortlej}", while the form with prefix encodes to "<a name="line.58"></a>
<span class="sourceLineNo">059</span> * {@code dortlaj|dortlej}". Thus, the full, combined encoding is "{@code (ortlaj|ortlej)-(dortlaj|dortlej)}".<a name="line.59"></a>
<span class="sourceLineNo">060</span> * &lt;p&gt;<a name="line.60"></a>
<span class="sourceLineNo">061</span> * The encoded forms are often quite a bit longer than the input strings. This is because a single input may have many<a name="line.61"></a>
<span class="sourceLineNo">062</span> * potential phonetic interpretations. For example, "{@code Renault}" encodes to "<a name="line.62"></a>
<span class="sourceLineNo">063</span> * {@code rYnDlt|rYnalt|rYnult|rinDlt|rinalt|rinult}". The {@code APPROX} rules will tend to produce larger<a name="line.63"></a>
<span class="sourceLineNo">064</span> * encodings as they consider a wider range of possible, approximate phonetic interpretations of the original word.<a name="line.64"></a>
<span class="sourceLineNo">065</span> * Down-stream applications may wish to further process the encoding for indexing or lookup purposes, for example, by<a name="line.65"></a>
<span class="sourceLineNo">066</span> * splitting on pipe ({@code |}) and indexing under each of these alternatives.<a name="line.66"></a>
<span class="sourceLineNo">067</span> * &lt;p&gt;<a name="line.67"></a>
<span class="sourceLineNo">068</span> * &lt;b&gt;Note&lt;/b&gt;: this version of the Beider-Morse encoding is equivalent with v3.4 of the reference implementation.<a name="line.68"></a>
<span class="sourceLineNo">069</span> * &lt;/p&gt;<a name="line.69"></a>
<span class="sourceLineNo">070</span> * @see &lt;a href="http://stevemorse.org/phonetics/bmpm.htm"&gt;Beider-Morse Phonetic Matching&lt;/a&gt;<a name="line.70"></a>
<span class="sourceLineNo">071</span> * @see &lt;a href="http://stevemorse.org/phoneticinfo.htm"&gt;Reference implementation&lt;/a&gt;<a name="line.71"></a>
<span class="sourceLineNo">072</span> *<a name="line.72"></a>
<span class="sourceLineNo">073</span> * &lt;p&gt;<a name="line.73"></a>
<span class="sourceLineNo">074</span> * This class is Not ThreadSafe<a name="line.74"></a>
<span class="sourceLineNo">075</span> * &lt;/p&gt;<a name="line.75"></a>
<span class="sourceLineNo">076</span> * @since 1.6<a name="line.76"></a>
<span class="sourceLineNo">077</span> */<a name="line.77"></a>
<span class="sourceLineNo">078</span>public class BeiderMorseEncoder implements StringEncoder {<a name="line.78"></a>
<span class="sourceLineNo">079</span>    // Implementation note: This class is a spring-friendly facade to PhoneticEngine. It allows read/write configuration<a name="line.79"></a>
<span class="sourceLineNo">080</span>    // of an immutable PhoneticEngine instance that will be delegated to for the actual encoding.<a name="line.80"></a>
<span class="sourceLineNo">081</span><a name="line.81"></a>
<span class="sourceLineNo">082</span>    // a cached object<a name="line.82"></a>
<span class="sourceLineNo">083</span>    private PhoneticEngine engine = new PhoneticEngine(NameType.GENERIC, RuleType.APPROX, true);<a name="line.83"></a>
<span class="sourceLineNo">084</span><a name="line.84"></a>
<span class="sourceLineNo">085</span>    @Override<a name="line.85"></a>
<span class="sourceLineNo">086</span>    public Object encode(final Object source) throws EncoderException {<a name="line.86"></a>
<span class="sourceLineNo">087</span>        if (!(source instanceof String)) {<a name="line.87"></a>
<span class="sourceLineNo">088</span>            throw new EncoderException("BeiderMorseEncoder encode parameter is not of type String");<a name="line.88"></a>
<span class="sourceLineNo">089</span>        }<a name="line.89"></a>
<span class="sourceLineNo">090</span>        return encode((String) source);<a name="line.90"></a>
<span class="sourceLineNo">091</span>    }<a name="line.91"></a>
<span class="sourceLineNo">092</span><a name="line.92"></a>
<span class="sourceLineNo">093</span>    @Override<a name="line.93"></a>
<span class="sourceLineNo">094</span>    public String encode(final String source) throws EncoderException {<a name="line.94"></a>
<span class="sourceLineNo">095</span>        if (source == null) {<a name="line.95"></a>
<span class="sourceLineNo">096</span>            return null;<a name="line.96"></a>
<span class="sourceLineNo">097</span>        }<a name="line.97"></a>
<span class="sourceLineNo">098</span>        return this.engine.encode(source);<a name="line.98"></a>
<span class="sourceLineNo">099</span>    }<a name="line.99"></a>
<span class="sourceLineNo">100</span><a name="line.100"></a>
<span class="sourceLineNo">101</span>    /**<a name="line.101"></a>
<span class="sourceLineNo">102</span>     * Gets the name type currently in operation.<a name="line.102"></a>
<span class="sourceLineNo">103</span>     *<a name="line.103"></a>
<span class="sourceLineNo">104</span>     * @return the NameType currently being used<a name="line.104"></a>
<span class="sourceLineNo">105</span>     */<a name="line.105"></a>
<span class="sourceLineNo">106</span>    public NameType getNameType() {<a name="line.106"></a>
<span class="sourceLineNo">107</span>        return this.engine.getNameType();<a name="line.107"></a>
<span class="sourceLineNo">108</span>    }<a name="line.108"></a>
<span class="sourceLineNo">109</span><a name="line.109"></a>
<span class="sourceLineNo">110</span>    /**<a name="line.110"></a>
<span class="sourceLineNo">111</span>     * Gets the rule type currently in operation.<a name="line.111"></a>
<span class="sourceLineNo">112</span>     *<a name="line.112"></a>
<span class="sourceLineNo">113</span>     * @return the RuleType currently being used<a name="line.113"></a>
<span class="sourceLineNo">114</span>     */<a name="line.114"></a>
<span class="sourceLineNo">115</span>    public RuleType getRuleType() {<a name="line.115"></a>
<span class="sourceLineNo">116</span>        return this.engine.getRuleType();<a name="line.116"></a>
<span class="sourceLineNo">117</span>    }<a name="line.117"></a>
<span class="sourceLineNo">118</span><a name="line.118"></a>
<span class="sourceLineNo">119</span>    /**<a name="line.119"></a>
<span class="sourceLineNo">120</span>     * Discovers if multiple possible encodings are concatenated.<a name="line.120"></a>
<span class="sourceLineNo">121</span>     *<a name="line.121"></a>
<span class="sourceLineNo">122</span>     * @return true if multiple encodings are concatenated, false if just the first one is returned<a name="line.122"></a>
<span class="sourceLineNo">123</span>     */<a name="line.123"></a>
<span class="sourceLineNo">124</span>    public boolean isConcat() {<a name="line.124"></a>
<span class="sourceLineNo">125</span>        return this.engine.isConcat();<a name="line.125"></a>
<span class="sourceLineNo">126</span>    }<a name="line.126"></a>
<span class="sourceLineNo">127</span><a name="line.127"></a>
<span class="sourceLineNo">128</span>    /**<a name="line.128"></a>
<span class="sourceLineNo">129</span>     * Sets how multiple possible phonetic encodings are combined.<a name="line.129"></a>
<span class="sourceLineNo">130</span>     *<a name="line.130"></a>
<span class="sourceLineNo">131</span>     * @param concat<a name="line.131"></a>
<span class="sourceLineNo">132</span>     *            true if multiple encodings are to be combined with a '|', false if just the first one is<a name="line.132"></a>
<span class="sourceLineNo">133</span>     *            to be considered<a name="line.133"></a>
<span class="sourceLineNo">134</span>     */<a name="line.134"></a>
<span class="sourceLineNo">135</span>    public void setConcat(final boolean concat) {<a name="line.135"></a>
<span class="sourceLineNo">136</span>        this.engine = new PhoneticEngine(this.engine.getNameType(),<a name="line.136"></a>
<span class="sourceLineNo">137</span>                                         this.engine.getRuleType(),<a name="line.137"></a>
<span class="sourceLineNo">138</span>                                         concat,<a name="line.138"></a>
<span class="sourceLineNo">139</span>                                         this.engine.getMaxPhonemes());<a name="line.139"></a>
<span class="sourceLineNo">140</span>    }<a name="line.140"></a>
<span class="sourceLineNo">141</span><a name="line.141"></a>
<span class="sourceLineNo">142</span>    /**<a name="line.142"></a>
<span class="sourceLineNo">143</span>     * Sets the type of name. Use {@link NameType#GENERIC} unless you specifically want phonetic encodings<a name="line.143"></a>
<span class="sourceLineNo">144</span>     * optimized for Ashkenazi or Sephardic Jewish family names.<a name="line.144"></a>
<span class="sourceLineNo">145</span>     *<a name="line.145"></a>
<span class="sourceLineNo">146</span>     * @param nameType<a name="line.146"></a>
<span class="sourceLineNo">147</span>     *            the NameType in use<a name="line.147"></a>
<span class="sourceLineNo">148</span>     */<a name="line.148"></a>
<span class="sourceLineNo">149</span>    public void setNameType(final NameType nameType) {<a name="line.149"></a>
<span class="sourceLineNo">150</span>        this.engine = new PhoneticEngine(nameType,<a name="line.150"></a>
<span class="sourceLineNo">151</span>                                         this.engine.getRuleType(),<a name="line.151"></a>
<span class="sourceLineNo">152</span>                                         this.engine.isConcat(),<a name="line.152"></a>
<span class="sourceLineNo">153</span>                                         this.engine.getMaxPhonemes());<a name="line.153"></a>
<span class="sourceLineNo">154</span>    }<a name="line.154"></a>
<span class="sourceLineNo">155</span><a name="line.155"></a>
<span class="sourceLineNo">156</span>    /**<a name="line.156"></a>
<span class="sourceLineNo">157</span>     * Sets the rule type to apply. This will widen or narrow the range of phonetic encodings considered.<a name="line.157"></a>
<span class="sourceLineNo">158</span>     *<a name="line.158"></a>
<span class="sourceLineNo">159</span>     * @param ruleType<a name="line.159"></a>
<span class="sourceLineNo">160</span>     *            {@link RuleType#APPROX} or {@link RuleType#EXACT} for approximate or exact phonetic matches<a name="line.160"></a>
<span class="sourceLineNo">161</span>     */<a name="line.161"></a>
<span class="sourceLineNo">162</span>    public void setRuleType(final RuleType ruleType) {<a name="line.162"></a>
<span class="sourceLineNo">163</span>        this.engine = new PhoneticEngine(this.engine.getNameType(),<a name="line.163"></a>
<span class="sourceLineNo">164</span>                                         ruleType,<a name="line.164"></a>
<span class="sourceLineNo">165</span>                                         this.engine.isConcat(),<a name="line.165"></a>
<span class="sourceLineNo">166</span>                                         this.engine.getMaxPhonemes());<a name="line.166"></a>
<span class="sourceLineNo">167</span>    }<a name="line.167"></a>
<span class="sourceLineNo">168</span><a name="line.168"></a>
<span class="sourceLineNo">169</span>    /**<a name="line.169"></a>
<span class="sourceLineNo">170</span>     * Sets the number of maximum of phonemes that shall be considered by the engine.<a name="line.170"></a>
<span class="sourceLineNo">171</span>     *<a name="line.171"></a>
<span class="sourceLineNo">172</span>     * @param maxPhonemes<a name="line.172"></a>
<span class="sourceLineNo">173</span>     *            the maximum number of phonemes returned by the engine<a name="line.173"></a>
<span class="sourceLineNo">174</span>     * @since 1.7<a name="line.174"></a>
<span class="sourceLineNo">175</span>     */<a name="line.175"></a>
<span class="sourceLineNo">176</span>    public void setMaxPhonemes(final int maxPhonemes) {<a name="line.176"></a>
<span class="sourceLineNo">177</span>        this.engine = new PhoneticEngine(this.engine.getNameType(),<a name="line.177"></a>
<span class="sourceLineNo">178</span>                                         this.engine.getRuleType(),<a name="line.178"></a>
<span class="sourceLineNo">179</span>                                         this.engine.isConcat(),<a name="line.179"></a>
<span class="sourceLineNo">180</span>                                         maxPhonemes);<a name="line.180"></a>
<span class="sourceLineNo">181</span>    }<a name="line.181"></a>
<span class="sourceLineNo">182</span><a name="line.182"></a>
<span class="sourceLineNo">183</span>}<a name="line.183"></a>




























































</pre>
</div>
</body>
</html>
